package br.com.news.activity;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import br.com.news.R;
import br.com.news.exception.NoResultException;
import br.com.news.exception.ServiceException;
import br.com.news.modal.CategoriaBean;
import br.com.news.service.CategoriaService;
import br.com.news.service.NoticiaService;
import br.com.news.service.interfaces.ICategoriaService;
import br.com.news.service.interfaces.INoticiaService;
import br.com.news.util.Constantes;
import br.com.news.util.DialogUtil;
import br.com.news.util.DialogUtil.ButtonDialog;

public class MainActivity extends GenericFragmentActivity {

	public Intent servico;
	
	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	ViewPager mViewPager;

	private ICategoriaService categoriaService;
	private INoticiaService noticiaService;

	private List<CategoriaBean> categoriaBeans;

	@Override
	protected int getContentView() {
		return R.layout.activity_main;
	}

	@Override
	protected void recuperarParametros() throws Exception {
	}

	@Override
	protected void preConstruct() throws Exception {
		istanciarObject();
		buscaCategorias();
		noticiaService.deleteAllNews();
		
		mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
	}

	@Override
	protected void recuperaElementosDoXml() throws Exception {
		mViewPager = (ViewPager) findViewById(R.id.pager);
	}

	@Override
	protected void instanciaElementosDoXml() throws Exception {
		mViewPager.setAdapter(mSectionsPagerAdapter);
	}

	@Override
	protected void defineAcaoDosElementos() throws Exception {
	}

	@Override
	protected void postConstruct() throws Exception {
		servico = new Intent("ATUALIZA_NOTICIA_SERVICO");
		startService(servico);
	}

	private void istanciarObject() {
		categoriaService = new CategoriaService(this);
		noticiaService = new NoticiaService(this);
	}

	private void buscaCategorias() {
		try {
			categoriaBeans = categoriaService.buscaCategoriasSelecionadas();
		} catch (ServiceException e) {
			DialogUtil.showDialog(this, getString(R.string.msg_falha_busc_cats), ButtonDialog.getButtonOk(this));
			Log.e(Constantes.LOG, e.getMessage(), e);
			finish();
		} catch (NoResultException e) {
			Log.e(Constantes.LOG, e.getMessage(), e);
			ErroActivity.chamaActivityErro(this, R.string.nenhuma_categoria_selecionada);
			finish();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	if(item.getItemId() == R.activity_main.menu_categoria){
    		Intent intent = new Intent(this, CategoriaActivity.class);
    		startActivity(intent);
    		finish();
    		return true;
    	}
    	return super.onMenuItemSelected(featureId, item);
    }
	
	@Override
	protected void onStart() {
		super.onStart();
		buscaCategorias();
	}

	/**
	 * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
	 * one of the primary sections of the app.
	 */
	public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int i) {
			Fragment fragment = new ListNoticiaFragment();
			Bundle args = new Bundle();
			args.putLong(ListNoticiaFragment.ID_CATEGORIA, categoriaBeans.get(i).getIdCategoria());

			fragment.setArguments(args);
			return fragment;
		}

		@Override
		public int getCount() {
			return categoriaBeans.size();
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return categoriaBeans.get(position).getDescricao();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (servico != null) {
			stopService(servico);
		}
		if (categoriaService != null && noticiaService != null) {
			categoriaService.fechar();
			noticiaService.fechar();
		}
	}
}
